package com.gzk.org.service.impl;

import com.gzk.basic.service.impl.BaseServiceImpl;
import com.gzk.org.domain.Department;
import com.gzk.org.mapper.DepartmentMapper;
import com.gzk.org.service.IDepartmentService;
import com.gzk.system.MyLog;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class DepartmentServiceImpl extends BaseServiceImpl<Department> implements IDepartmentService {

    @Autowired
    private DepartmentMapper departmentMapper;

    @Override
    @MyLog(description = "新增一个部门")
    public void add(Department department) {
        departmentMapper.save(department);
        this.update(department);
    }

    @Override
    @MyLog(description = "修改部门信息")
    public void update(Department department) {
        String dirPath = "";
        if(department.getParent() == null){
            dirPath = "/"+department.getId();
        }else {
            dirPath = departmentMapper.loadById(department.getParent().getId()).getDirPath() + "/" + department.getId();
        }
        department.setDirPath(dirPath);
        departmentMapper.update(department);
    }


    /**
     * 获取层级关系
     * @return
     */
    @Override
    @MyLog(description = "获取部门树")
    public List<Department> getTree() {
        return departmentMapper.getTree();
    }
}
